
from mpi4py import MPI
import scipy.sparse as sp
import scipy.linalg as la
import numpy as np

from problem import Problem
from analysis import analysis
from ioparser import parserModel, parserParam, parserQOI, parserFields
from stepManager import stepManager

PARALLEL_TESTS = True


def test_low_level(log=None):
    '''
        Test the basic features of the icafe library
    '''
    
    
    # check input file reading:
      
      ## unique input file:
    inputfile = "./test/beam/beam.dat"
    paramfile = "./test/beam/full-linear-static.param"
    
    parser = parserModel.parserPatran.Parser(inputfile)
    parser.read()
    
      ## load parameters from file
    pr = parserParam.paramReader(paramfile,log)
    print pr.paramObject
    
    domain, stpm = parser.buildModel() 
    
    # new increment manager:
    increment = stepManager.IncrementManager(sequence=[0.5,0.5],stepId=stpm.stepsMap['C2U'][0])
    
    stpm.setStepProperties(stpm.stepsMap['C2U'][0],incrementManager=increment)
    
    # check step management:
    print stpm
    
      ## set next step:
    stpm.setNextStep(domain)
      
      ## set next increment:
      
      
    # check mesh capabilities:
    mesh = domain.meshes[0]
    print mesh  
      ## assembling left and right hand side operators:

    #K, F = mesh.computeOperators(['K'],['Fext'])
      
      ## getting constraint operators:
    #constraints = mesh.getConstraints()  
    
    # check domain decomposition capabilities:
    print domain
      ## building boundaries:
    
      ## analyse
    domain.analyse()  
      
    # compare solution:
    mesh = domain.meshes[0]
    print mesh.quantities
    print mesh.fields
    
    # output:
    #vtkParser = parserFields.parserVtk(domain)
#    csvParser = parserQOI.ParserCSV(domain, 'QOI')    
#    csvParser.writeOutputFile()
    vtkParser = parserFields.ParserVTK(domain,'FIELDS')
    vtkParser.writeOutputFile()
     
    if log:  
        log.info('finished')
    

def test_beam_linear(log=None):
    '''
       Test the library for solving a linear static analysis of a beam model
    '''
    
    # declare input file:
    inputfile = "./test/beam.dat"
    paramfile = "./test/full-linear-static.param"
    
    # create the Problem instance
    p = Problem(inputfile,paramfile,log)
    
    # check and/or adjust parameters:
    
    # check and/or adjust steps:
    # p.manageSteps() # interactive
    p.stepManager.setStepAnalysis('default',analysis.LinearStatic)
    
    # start a simulation
    p.start()
    
    # save results
    p.save()
    
    return 0

def test_beam_dd_linear(parallel=PARALLEL_TESTS,log=None):
    '''
       Test the library for solving a linear static analysis of a decomposed beam model
    '''
    # declare input file:
    inputfile = "./test/beam-ddm.dat"
    paramfile = "./test/dd-linear-static.param"
    
    
    return 0

def test_beam_nonlinear(log=None):
    
    # declare input file:
    inputfile = "./test/beam-nl.dat"
    paramfile = "./test/full-nonlinear-static.param"
    
    # create the Problem instance
    p = Problem(inputfile,paramfile)
    
    # check and/or adjust parameters:
    
    # start a simulation
    p.start()
    
    # save results
    
    
    return 0

def test_beam_dd_nonlinear(parallel=PARALLEL_TESTS,log=None):
    
    # declare input file:
    inputfile = "./test/beam-ddm-nl.dat"
    paramfile = "./test/full-linear-static.param"
    
    # create the Problem instance
    p = Problem(inputfile,paramfile)
    
    # check and/or adjust parameters:
    
    # start a simulation
    p.start()
    
    # save results
    p.save()
    
    
    return 0


def test_beam_PBAMR(log=None):
    
    
    # declare input file:
    inputfile = "./test/beam.dat"
    paramfile = "./test/full-linear-static.param"
    
    # create the Problem instance
    p = Problem(inputfile,paramfile)
    
    # check and/or adjust parameters:
    
    # start a simulation
    p.start()
    
    # save results
    
    
    return 0

if __name__=="__main__":
    import logging
    
    log = logging.getLogger("test-low-level")
    log.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter('%(levelname)s: fname=%(filename)s lineNo=%(lineno)d %(message)s')
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    log.addHandler(ch)
    test_low_level(log)

    log.info("test succeeded")

    
#    try:
#        test_beam_dd_linear(log)
#        log.info("- test_beam_dd_linear succeeded")
#    except:
#        log.warning("- test_beam_dd_linear failed")


